package com.hxkj.service.impl;


import com.hxkj.client.service.NavigationServiceClient;
import com.hxkj.domain.enums.NavigationExceptionEnum;
import com.hxkj.domain.po.Navigation;
import com.hxkj.domain.po.NavigationTree;
import com.hxkj.service.NavigationService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 用户mapper
 * <p>
 * Created by chenfangzhou on 2018/5/15.
 */
@Service
@Slf4j
public class NavigationServiceImpl implements NavigationService {


    @Autowired
    private NavigationServiceClient navigationServiceClient;


    /**
     * 保存栏目
     *
     * @param navigation
     * @return
     * @throws Exception
     */
    @Override
    public ResultDataStruct saveNavigation(Navigation navigation) throws Exception {
        if (navigation == null) {
            throw new CustomException(NavigationExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultDataStruct result = new ResultDataStruct();
        try {
            // 查询站点列表
            result = navigationServiceClient.saveNavigation(navigation);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(NavigationExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(NavigationExceptionEnum.getEnumByCode(result.getCode()));

        }
    }

    /**
     * 查询单条栏目
     *
     * @param navigation
     * @return
     * @throws Exception
     */
    @Override
    public ResultDataStruct<Navigation> getNavigation(Navigation navigation) throws Exception {
        if (navigation == null) {
            throw new CustomException(NavigationExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultDataStruct<Navigation> result = new ResultDataStruct();
        try {
            result = navigationServiceClient.getNavigation(navigation);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(NavigationExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(NavigationExceptionEnum.getEnumByCode(result.getCode()));
        }
    }

    /**
     * 查询栏目集合
     *
     * @param navigation
     * @param
     * @return
     * @throws Exception
     */
    @Override
    public ResultDataStruct<List<NavigationTree>> getNavigationList(Navigation navigation) throws Exception {
        ResultDataStruct<List<NavigationTree>> result;
        try {
            // 查询站点列表
            result = navigationServiceClient.getNavigationList(navigation);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(NavigationExceptionEnum.getEnumByCode(result.getCode()));
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw e;

        }

    }


    /**
     * 修改栏目
     *
     * @param navigation
     * @return
     * @throws Exception
     */
    @Override
    public ResultMessageStruct updateNavigation(Navigation navigation) throws Exception {
        if (navigation == null) {
            throw new CustomException(NavigationExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            result = navigationServiceClient.updateNavigation(navigation);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(NavigationExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(NavigationExceptionEnum.getEnumByCode(result.getCode()));
        }
    }

    /**
     * 删除栏目
     *
     * @param navigation
     * @return
     * @throws Exception
     */

    @Override
    public ResultMessageStruct deleteNavigation(Navigation navigation) throws Exception {
        if (navigation == null) {
            throw new CustomException(NavigationExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            result = navigationServiceClient.deleteNavigation(navigation);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(NavigationExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(NavigationExceptionEnum.getEnumByCode(result.getCode()));
        }
    }

    @Override
    public ResultMessageStruct modifyOrderIds(Map<String, Object> map) throws Exception {
        if (map == null) {
            throw new CustomException(NavigationExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            result = navigationServiceClient.modifyOrderId(map);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(NavigationExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (CustomException e) {
            throw e;
        } catch (Exception e) {
            throw new CustomException(NavigationExceptionEnum.getEnumByCode(result.getCode()));
        }
    }


}
